//
//  DXWOutsideLiveController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/9/3.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWOutsideLiveController.h"
#import "DXWYuLeFlowLayout.h"
#import "DXWYuLeCell.h"
#import <MJExtension/MJExtension.h>
#import "DXWYuLeCellItem.h"

@interface DXWOutsideLiveController ()<UICollectionViewDelegate, UICollectionViewDataSource>


/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;

/** 游戏流水布局 */
@property (nonatomic, strong) DXWYuLeFlowLayout *layout;
/** 房间信息 */
@property (nonatomic, strong) NSArray *data;
/** 记录总共有多少直播间 */
@property (nonatomic, assign) NSInteger *total;
@end

@implementation DXWOutsideLiveController


static NSString * const  ID = @"yuLeCell";
//懒加载 -- 游戏CollectionView
-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:self.layout];
        
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

//懒加载 -- 游戏流水布局
- (DXWYuLeFlowLayout *)layout
{
    if (_layout == nil) {
        _layout = [[DXWYuLeFlowLayout alloc]init];
    }
    return _layout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.contentInset = UIEdgeInsetsMake(64 + 40 + 5, 0, 49, 0);
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"DXWYuLeCell" bundle:nil] forCellWithReuseIdentifier:ID];
    [self loadData];
    
}
#pragma mark -加载网络数据
- (void)loadData
{
    /*
     熊猫星秀
     http://api.m.panda.tv/ajax_get_live_list_by_cate?cate=yzdr&pageno=1&pagenum=10&order=person_num&status=2&banner=1&__version=2.0.1.1339&__plat=ios&__channel=appstore
     户外直播
     http://api.m.panda.tv/ajax_get_live_list_by_cate?cate=hwzb&pageno=1&pagenum=10&order=person_num&status=2&banner=1&__version=2.0.1.1339&__plat=ios&__channel=appstore
     萌宠乐园
     http://api.m.panda.tv/ajax_get_live_list_by_cate?cate=pets&pageno=1&pagenum=10&order=person_num&status=2&banner=1&__version=2.0.1.1339&__plat=ios&__channel=appstore
     */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager dxw_HTTPManager];
    NSDictionary *dict = @{
                           @"cate" : @"hwzb",
                           @"pageno" : @"1",
                           @"pagenum" : @"84",
                           @"order" : @"person_num",
                           @"status" : @"2",
                           @"banner" : @"1",
                           @"__version" :dxw_version,
                           @"__plat" : @"ios",
                           @"__channel" : @"appstore"
                           };
    [manager GET:@"http://api.m.panda.tv/ajax_get_live_list_by_cate?" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _data = [DXWYuLeCellItem  mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
        
        NSLog(@"%@",_data);
        
        //处理缺口
        [_collectionView reloadData];
        
        
        //        [responseObject writeToFile:@"/Users/milkteacat/Desktop/淡/蛙喵项目练习/熊猫TV/萌宠乐园.plist" atomically:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DXWYuLeCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.cellItem = _data[indexPath.row];
    return cell;
}
@end
